use tauri::{
    tray::{MouseButton, TrayIconEvent},
    AppHandle, Manager,
};

// use tauri::{
//     image::Image,
//     menu::{Menu, MenuItem},
//     tray::TrayIconBuilder,
//     App,
// };

/// Creating tray from rust can avoid creating after each frontend reloading.
/// But with release build user is no way to reload the app. So creating tray icon from frontend is accpetable.
/// Aslo creating tray icon from rust is hard to leverage i18n from frontend.
// pub fn new(app: &App) {
//     Image::from_path("icons/tray-icon.png")
//         .and_then(|icon| {
//             let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
//             let menu = Menu::with_items(app, &[&quit_i])?;

//             TrayIconBuilder::new()
//                 .menu(&menu)
//                 .on_menu_event(|app, event| match event.id.as_ref() {
//                     "quit" => app.exit(0),
//                     _ => println!("menu item {:?} not handled", event.id),
//                 })
//                 .show_menu_on_left_click(false)
//                 .icon(icon)
//                 .build(app)
//         })
//         .and_then(|tray| Ok(()))
//         .unwrap_or_else(|_| eprintln!("Failed to create tray icon"));
// }

pub fn handle_tray_icon_event(app: &AppHandle, event: TrayIconEvent) {
    if let TrayIconEvent::Click { button, .. } = event {
        if button == MouseButton::Left {
            app.get_webview_window("main").and_then(|window| {
                window.show().ok();
                window.set_focus().ok();

                Some(())
            });
        }
    }
}
